package com.example.nutritionmanager.record

import java.time.LocalTime

object MealType {
    const val MEAL_TYPE_BREAKFAST: Int = 0
    const val MEAL_TYPE_LUNCH: Int = 1
    const val MEAL_TYPE_DINNER: Int = 2
    const val MEAL_TYPE_SNACK: Int = 3
    const val MEAL_TYPE_OTHER: Int = 4
}
// 时间段配置类（数据类）
data class TimeRange(
    val startHour: Int,
    val startMinute: Int = 0,
    val endHour: Int,
    val endMinute: Int = 0
)
// 餐别时间配置（可自定义）
private val mealTimeConfig = mapOf(
    MealType.MEAL_TYPE_BREAKFAST to TimeRange(5, 0, 10, 59),   // 05:00 - 10:59
    MealType.MEAL_TYPE_LUNCH to TimeRange(11, 0, 15, 59),      // 11:00 - 15:59
    MealType.MEAL_TYPE_DINNER to TimeRange(16, 0, 20, 59),     // 16:00 - 20:59
    MealType.MEAL_TYPE_SNACK to TimeRange(21, 0, 4, 59)        // 21:00 - 次日04:59
)

// 核心判断逻辑
fun getCurrentMealType(currentTime: LocalTime = LocalTime.now()): Int {
    // 将当前时间转换为分钟数（便于处理跨天）
    val currentMinutes = currentTime.hour * 60 + currentTime.minute

    // 遍历所有配置项进行检查
    mealTimeConfig.forEach { (mealType, timeRange) ->
        val startMinutes = timeRange.startHour * 60 + timeRange.startMinute
        var endMinutes = timeRange.endHour * 60 + timeRange.endMinute

        // 处理跨天时间（如宵夜时段）
        if (endMinutes < startMinutes) endMinutes += 24 * 60

        // 判断当前时间是否在该时间段内
        if (currentMinutes in startMinutes..endMinutes ||
            (currentMinutes + 24*60) in startMinutes..endMinutes) {
            return mealType
        }
    }

    // 默认返回（理论上不会执行到这里）
    return MealType.MEAL_TYPE_OTHER
}